package hk.m4s.lr.repair.test.ui.user;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.AddressModel;
import hk.m4s.lr.repair.test.model.AddressModelResult;
import hk.m4s.lr.repair.test.model.DeviceModel;
import hk.m4s.lr.repair.test.model.DeviceModelResult;
import hk.m4s.lr.repair.test.service.address.AddressService;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.ui.adapter.AddressListAdapter;
import hk.m4s.lr.repair.test.ui.adapter.EquipmentListAdapter;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/***
 * 管理收货地址
 *
 * **/
public class ShopAddressActivity extends UeBaseActivity implements View.OnClickListener,VListView.IXListViewListener{
	private Context context;
	private ImageButton backs;
	private Button add;
	private VListView shop_listView;
	private AddressListAdapter adpter;
	private List<AddressModel> list=new ArrayList<AddressModel>();;
	private LinearLayout show_img;
	private TextView shop_adrress_check,shop_et,shop_delete;
	private String checked="1";
	private int pageNum = 0;
	private int pageSize = 10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		baseSetContentView(R.layout.activity_shop_address);
		hiddenFooter();
		hiddenNewMessage();
		showGoBackBtn();
		setTitleText("管理收货地址");
		context=this;
		initView();

	}
	public void initView(){

		add=(Button)findViewById(R.id.add);
		shop_listView=(VListView)findViewById(R.id.address_list);
		add.setOnClickListener(this);
		adpter=new AddressListAdapter(ShopAddressActivity.this,list);
		shop_listView.setAdapter(adpter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.show_backs:
				finish();
				break;
			case R.id.add:
				Intent nin=new Intent(ShopAddressActivity.this,AddAddressActivity.class);
				nin.putExtra("title_name","添加新地址");
				startActivity(nin);
				break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		getData("1");

	}

	public void getData(final String refreshType){
		Map<String, Object> parmas = new HashMap<>();
		parmas.put("device_type", Constant.device_type);
		parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
		try {
			JSONObject jsondata = new JSONObject();
			jsondata.put("pageNum", pageNum);
			jsondata.put("pageSize", pageSize);
			parmas.put("jsonText",jsondata.toString());
		}catch (JSONException e){

		}
		AddressService.expressAddressList(context,parmas , new ResponseCallback<AddressModelResult>() {
			@Override
			public void onSuccess(AddressModelResult deviceModelResult) {
				onLoad();
				if(deviceModelResult.getList()!=null) {
					switch (refreshType) {
						case "1":
							list.clear();
							list.addAll(deviceModelResult.getList());
							if (deviceModelResult.getList().size() <= pageSize) {
								shop_listView.removeFooterView();
								shop_listView.setPullLoadEnable(false);
							} else {
								shop_listView.showFooterView();
								shop_listView.setPullLoadEnable(true);
							}
							break;
						case "2":
							list.addAll(deviceModelResult.getList());
							if (deviceModelResult.getList().size() <= pageSize) {
								shop_listView.removeFooterView();
								shop_listView.setPullLoadEnable(false);
							}
							break;
					}
					adpter.notifyDataSetChanged();
				}
			}

			@Override
			public void onError(Response response, int errorCode, String errorMsg, Exception e) {

			}
		});
	}
	private void onLoad() {
		shop_listView.stopRefresh();
		shop_listView.stopLoadMore();
		Date data = new Date(System.currentTimeMillis());
		shop_listView.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINA).format(data));
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		// getData();
		shop_listView.setPullLoadEnable(true);
		pageNum=0;
		getData("1");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		shop_listView.setPullRefreshEnable(true);
		pageNum++;
		getData("2");
	}
}
